home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
HPAVC
/
HPAVC CD-ROM.iso
/
MCASM.RAR
/
MC_ASM.EXE
/
WROX_ASM
/
CH10
/
PROGRAMS
/
ARCH.C
next >
Wrap
C/C++ Source or Header
|
1994-05-28
|
9KB
|
410 lines
/* Demo compression program */
/* Malakhov K.A. 1994 */
/* The compressed data is writes by blocks with size BUFSIZE.*/
/* The structure of block:
Fields Meaning
WORD length of code field
BYTE 'C' or 'U' - compressed or uncompressed code
BYTES code
*/
#include <stdio.h>
#include <dos.h>
#define BUFSIZE 30000
extern int rle_encode(char*,int,char*);
extern int rle_decode(char*,int,char*);
extern int lzw_encode(char*,int,char*);
extern int lzw_decode(char*,int,char*);
extern int haff_encode(char*,int,char*,int);
extern int haff_decode(char*,int,char*,int);
extern int arif_encode(char*,int,char*,int);
extern int arif_decode(char*,int,char*,int);
void *malloc();
void main(int argc, char *argv[])
{
FILE *input_file, *output_file;
char input_file_name[81],output_file_name[81];
char *buffi,*buffo;
int *bfi,*bfo;
int fsize,fsize1;
char ch;
// Allocate memory
buffi=malloc(BUFSIZE+3);
buffo=malloc(BUFSIZE+3);
bfi=(int *) buffi;
bfo=(int *) buffo;
// Check allocating results
if (buffi==NULL) {
printf("Error allocating buffer's space!\n");
exit(1);
}
if (buffo==NULL) {
printf("Error allocating buffer's space!\n");
free(buffi);
exit(1);
}
// Check parameter string
if ((argc!=5) || (strpbrk(argv[1],"RrZzHhAa")==NULL) ||\
(strpbrk(argv[2],"EeDd")==NULL))
{
printf("arch.exe method option infile outfile\n");
printf(" method:\n");
printf(" r - RLE\n");
printf(" z - LZW\n");
printf(" h - Haffman\n");
printf(" a - Arifmetic\n");
printf(" option:\n");
printf(" e - encode\n");
printf(" d - decode\n");
free(buffi);
free(buffo);
exit(0);
}
else {
strcpy(input_file_name,argv[3]);
strcpy(output_file_name,argv[4]);
}
// Open files
input_file=fopen(input_file_name,"rb");
output_file=fopen(output_file_name,"wb");
if (input_file == NULL || output_file == NULL) {
printf("Error opening files\n");
free(buffi);
free(buffo);
exit(1);
}
printf("Wait...\n");
// Check methods
// RLE RLE RLE RLE RLE RLE RLE RLE RLE RLE RLE RLE RLE RLE RLE RLE RLE RLE RLE
if (strpbrk(argv[1],"Rr")!=NULL) // RLE
{
if (strpbrk(argv[2],"Ee")!=NULL) // Encode
{
// Main loop
while
((fsize=fread(buffi+3,sizeof(char),BUFSIZE,input_file))!=0)
{
bfi[0]=fsize; // Store the size of data
if ((fsize1=rle_encode(buffi+3,fsize,buffo+3))!=0)
{
// Compressed
bfo[0]=fsize1;
buffo[2]='C';
fwrite(buffo,sizeof(char),fsize1+3,\
output_file);
}
else {
// Uncompressed
buffi[2]='U';
fwrite(buffi,sizeof(char),fsize+3,\
output_file);
}
}
}
else //Decode
{
// Main loop
while ((fsize=fread(buffi,sizeof(char),3,input_file))==3)
{
// Read block
fsize=bfi[0];
ch=buffi[2];
fsize1=fread(buffi,sizeof(char),fsize,input_file);
// Check data structure
if (fsize==fsize1)
{
if (ch=='C')
{
// Compressed
fsize=rle_decode(buffi,fsize1,buffo);
if (fsize==0)
{
printf("Internal RLE error\n");
free(buffi);
free(buffo);
fclose(input_file);
fclose(output_file);
exit(1);
}
fwrite(buffo,sizeof(char),fsize,output_file);
}
else
// Uncompressed
fwrite(buffi,sizeof(char),fsize,output_file);
}
else {
// File structure error
printf("Error in compressed file\n");
free(buffi);
free(buffo);
fclose(input_file);
fclose(output_file);
exit(1);
}
}
if (fsize!=0) {
// File structure error
printf("Error in compressed file\n");
free(buffi);
free(buffo);
fclose(input_file);
fclose(output_file);
exit(1);
}
}
}
//---------------------------------------------------------------------------
else if (strpbrk(argv[1],"Zz")!=NULL) //LZW
{
if (strpbrk(argv[2],"Ee")!=NULL) // Encode
{
// Main loop
while
((fsize=fread(buffi+3,sizeof(char),BUFSIZE,input_file))!=0)
{
bfi[0]=fsize; // Store the size of data
if ((fsize1=lzw_encode(buffi+3,fsize,buffo+3))!=0)
{
// Compressed
bfo[0]=fsize1;
buffo[2]='C';
fwrite(buffo,sizeof(char),fsize1+3,\
output_file);
}
else {
// Uncompressed
buffi[2]='U';
fwrite(buffi,sizeof(char),fsize+3,\
output_file);
}
}
}
else //Decode
{
// Main loop
while ((fsize=fread(buffi,sizeof(char),3,input_file))==3)
{
// Read block
fsize=bfi[0];
ch=buffi[2];
fsize1=fread(buffi,sizeof(char),fsize,input_file);
// Check data structure
if (fsize==fsize1)
{
if (ch=='C')
{
// Compressed
fsize=lzw_decode(buffi,fsize1,buffo);
if (fsize==0)
{
printf("Internal RLE error\n");
free(buffi);
free(buffo);
fclose(input_file);
fclose(output_file);
exit(1);
}
fwrite(buffo,sizeof(char),fsize,output_file);
}
else
// Uncompressed
fwrite(buffi,sizeof(char),fsize,output_file);
}
else {
// File structure error
printf("Error in compressed file\n");
free(buffi);
free(buffo);
fclose(input_file);
fclose(output_file);
exit(1);
}
}
if (fsize!=0) {
// File structure error
printf("Error in compressed file\n");
free(buffi);
free(buffo);
fclose(input_file);
fclose(output_file);
exit(1);
}
}
}
// HAFF HAFF HAFF HAFF HAFF HAFF HAFF HAFF HAFF HAFF HAFF HAFF HAFF HAFF HAFF
else if (strpbrk(argv[1],"Hh")!=NULL) // Haffman
{
if (strpbrk(argv[2],"Ee")!=NULL) // Encode
{
// Main loop
while
((fsize=fread(buffi+3,sizeof(char),BUFSIZE,input_file))!=0)
{
bfi[0]=fsize;
if ((fsize1=haff_encode(buffi+3,fsize,buffo+3,\
BUFSIZE))!=0)
{
// Compressed
bfo[0]=fsize1;
buffo[2]='C';
fwrite(buffo,sizeof(char),fsize1+3,\
output_file);
}
else {
// Uncompressed
buffi[2]='U';
fwrite(buffi,sizeof(char),fsize+3,\
output_file);
}
}
}
else //Decode
{
// Main loop
while ((fsize=fread(buffi,sizeof(char),3,input_file))==3)
{
// Read block
fsize=bfi[0];
ch=buffi[2];
fsize1=fread(buffi,sizeof(char),fsize,input_file);
// Check data structure
if (fsize==fsize1)
{
if (ch=='C')
{
// Compressed
fsize=haff_decode(buffi,fsize1,buffo,\
BUFSIZE+3);
if (fsize==0)
{
printf("Internal Haffman error\n");
free(buffi);
free(buffo);
fclose(input_file);
fclose(output_file);
exit(1);
}
fwrite(buffo,sizeof(char),fsize,output_file);
}
else
// Uncompressed
fwrite(buffi,sizeof(char),fsize,output_file);
}
else {
// File structure error
printf("Error in compressed file\n");
free(buffi);
free(buffo);
fclose(input_file);
fclose(output_file);
exit(1);
}
}
if (fsize!=0) {
// File structure error
printf("Error in compressed file\n");
free(buffi);
free(buffo);
fclose(input_file);
fclose(output_file);
exit(1);
}
}
}
//---------------------------------------------------------------------------
else if (strpbrk(argv[1],"Aa")!=NULL) // Arifmetic
{
if (strpbrk(argv[2],"Ee")!=NULL) // Encode
{
// Main loop
while
((fsize=fread(buffi+3,sizeof(char),BUFSIZE,input_file))!=0)
{
bfi[0]=fsize; // Store the size of data
if ((fsize1=arif_encode(buffi+3,fsize,buffo+3,\
BUFSIZE))!=0)
{
// Compressed
bfo[0]=fsize1;
buffo[2]='C';
fwrite(buffo,sizeof(char),fsize1+3,\
output_file);
}
else {
// Uncompressed
buffi[2]='U';
fwrite(buffi,sizeof(char),fsize+3,\
output_file);
}
}
}
else //Decode
{
// Main loop
while ((fsize=fread(buffi,sizeof(char),3,input_file))==3)
{
// Read block
fsize=bfi[0];
ch=buffi[2];
fsize1=fread(buffi,sizeof(char),fsize,input_file);
// Check data structure
if (fsize==fsize1)
{
if (ch=='C')
{
// Compressed
fsize=arif_decode(buffi,fsize1,buffo,BUFSIZE);
if (fsize==0)
{
printf("Internal Arifmetic error\n");
free(buffi);
free(buffo);
fclose(input_file);
fclose(output_file);
exit(1);
}
fwrite(buffo,sizeof(char),fsize,output_file);
}
else
// Uncompressed
fwrite(buffi,sizeof(char),fsize,output_file);
}
else {
// File structure error
printf("Error in compressed file\n");
free(buffi);
free(buffo);
fclose(input_file);
fclose(output_file);
exit(1);
}
}
if (fsize!=0) {
// File structure error
printf("Error in compressed file\n");
free(buffi);
free(buffo);
fclose(input_file);
fclose(output_file);
exit(1);
}
}
}
fclose(input_file);
fclose(output_file);
free(buffi);
free(buffo);
}